<template>
  <a-config-provider :locale="languages[lang]"> 
    <div id="app">
      <router-view/>
    </div>
  </a-config-provider>
</template>

<script>
import { mapState } from 'vuex'
import { languages } from '@/locales/index'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import 'dayjs/locale/en'
import { message } from 'ant-design-vue' 
export default {
  data () {
    return {
      languages,
      wsSocket: null,
      stompClient: null,
      reloadPage: true
    }
  },
  computed: {
    ...mapState({
      lang: state => state.setting.lang, 
    })
  },
  watch: {
    lang: {
      handler (language) {
        console.log("🚀🚀🚀🚀🚀", language)
        if (!language) return;
        let lang = language === 'zh-cn' ? language : 'en_US'
        dayjs.locale(lang)
        this.$i18n.locale = language
      },
      immediate: true
    }
  },
  methods: {
    currentTime () {
      return Date.now();
    },
    onUsbEvent () {
      let flags = [];
      let startTime = null;
      const scancodeLen = 10;
      const scancodeTime = 200;

      window.onkeydown = (e) => {
        if (!flags.length  && ( e.key && e.key.length == 1)) {
          flags.push(e.key);
          startTime = this.currentTime()
          return;
        }
        const diff = this.currentTime() - startTime;
        if (flags.length < scancodeLen && diff <= scancodeTime) {
          flags.push(e.key);
          return;
        }
        if (flags.length === scancodeLen && diff <= scancodeTime) {
          let result = ''
          // const result = flags.join("");
          result = flags.join("");
          try {
            console.log("app -- result", result);
            if (result.length == 10) {
              if (this.$route.path != '/hangManage/hangerInfo') {
                this.$router.push({
                  path: '/hangManage/hangerInfo', query: { no: result }
                })
              }
            }
          } catch (err) {
            console.log("🚀 ~ file: usb-reader copy.js ~ line 18 ~ err", err);
          }
          flags = [];
          startTime = null;
          return;
        }

        if (diff > scancodeTime) {
          flags = [];
          startTime = null;
          return;
        }
      };
    }
  },
  mounted () {
    message.config({ top: '15%' })
    this.onUsbEvent()
  }
}
</script>